<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Inverse kinematics tutorial</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>

<body>

<div align="center">
<table class=allEncompassingTable >
 <tr>
  <td >
<p><a href="../index.html" TARGET="_top"><img src="images/homeImg.png"></a></p>



<h1>Inverse kinematics tutorial</h1>

<p>This tutorial will try to explain how to use the <a href="inverseKinematicsModule.htm">inverse kinematics functionality</a>, while building a 7 DoF redundant manipulator. But before that, make sure  to have a look at the various simple example scenes related to IK and FK in folder <em>scenes/ik_fk_simple_examples</em>. For this tutorial, we will build a non-dynamic manipulator, that just uses inverse kinematics without using any physics engine functionality. The CoppeliaSim CAD data related to this tutorial (&quot;redundantManipulator.stl&quot;) is located in CoppeliaSim's installation folder's &quot;cadFiles&quot; folder. A CoppeliaSim <a href="scenes.htm">scene</a> related to this tutorial can be found in CoppeliaSim's installation folder's &quot;tutorials\InverseKinematics&quot; folder. Click [Menu bar --&gt; File --&gt; Import --&gt; Mesh...] then select the file to import. Also refer to the section on how to <a href="importExport.htm">import/export shapes</a>. A dialog pops open asking about mesh scaling and mesh orientation. Click Ok. A single <a href="shapes.htm">simple shape</a> was imported and is located in the middle of the scene. The shape also appears in the <a href="userInterface.htm#SceneHierarchy">scene hierarchy</a> on the left hand side of the main window. Depending on how the original CAD data was exported, the imported CAD data could be at a different scale, different location, or even subdivided into several shapes. The assigned color of imported shapes is random. Following figure shows the imported shape:<br>
</p>


<p align=center><img src="images/ikTut1.jpg"></p>
<br>

<p>As you can see, the import operation has left us with a single shape, where we expected several shapes. This means that we will have to divide the manipulator object ourselves: select the <a href="objects.htm">object</a> (just click on it in the scene or the scene hierarchy), then click [Menu bar --&gt; Edit --&gt; Grouping/Merging --&gt; Divide selected shapes]. Following is what you should have:<br>
</p>

<p align=center><img src="images/ikTut2.jpg"></p>
<br>

<p>The original shape was divided into several sub-shapes (see also the scene hierarchy). The shape division algorithm operates by grouping all triangles that are linked by common edges. Depending on how the original mesh was created or exported, such a division procedure cannot be performed. In that case you will have to manually extract shapes in the <a href="triangleEditMode.htm">triangle edit mode</a>. <br>
</p>

<p>Next, we will change colors of the various objects so as to have a nice visual appearance. First double-click a shape icon in the scene hierarchy. The <a href="shapeProperties.htm">shape properties</a> dialog opens. While a shape is selected, click on <strong>Adjust outside color</strong> in the dialog: this will allow you to adjust the various color components of the outside faces of the selected shape. For now, just adjust the ambient/diffuse color component of your shapes. To transfer the color of one shape to another shape, select both shapes and make sure the last selected shape (indicated with a white bounding box) is the one you want to take the color from, then simply click the <strong>Apply to selection</strong> button in the <strong>Colors</strong> section of the shape dialog. Feel free to adjust other visual parameters too, like the <strong>Shading angle</strong> parameter, the <strong>Edges width</strong> or the <strong>Edges color</strong>. Once you finished coloring, you might have following situation:<br>
</p>

<p align=center><img src="images/ikTut3.jpg"></p>
<br>

<p>In next step, we will add the 7 <a href="joints.htm">joints</a> of the manipulator. One way of doing this is to add the joints into the scene, then specify their appropriate position and orientation (through the <a href="positionDialog.htm">position dialog</a> and the <a href="orientationDialog.htm">orientation dialog</a>). This is however not possible, when you don't know the exact joint positions as in our case, and so we will have to <em>extract</em> them from the shapes that we have:<br>
</p>

<p>Select all imported shapes and click [Menu bar --&gt; Edit --&gt; Bounding box alignment --&gt; Align selected shapes' coordinate frame with world]. This operation guarantees that our <a href="shapeReferenceFrames.htm">bounding boxes</a> are aligned with the absolute reference frame, and given the current manipulator configuration, represents the smallest bounding boxes. Click [Menu bar --&gt; Add --&gt; Joint --&gt; Revolute] to insert a revolute joint into the scene. The default position is at (0;0;0) and its default orientation is vertical, and so the joint is hidden by the manipulator's base cylinder. While the joint is still selected, ctrl-select the base cylinder, then open the <a href="positionDialog.htm">position dialog</a> on the <strong>position</strong> tab and click the <strong>Apply to selection</strong>. This just positioned the joint at the exact same coordinates as the base cylinder (this operation however only slightly adjusted the joint's vertical position since it was already almost in position). Now repeat the procedure for all other joints in the manipulator (remember there should be a total of 7). All joints are in position now, however, some of them have the wrong orientation. Select all joints that should be aligned with the world's Y-axis, then enter (90,0,0) for the <strong>Alpha</strong>, <strong>Beta</strong> and <strong>Gamma</strong> items in the <a href="orientationDialog.htm">orientation dialog</a>, on the <strong>orientation</strong> tab,  then click the <strong>Apply to selection</strong> button. Next, select the joint that should be aligned with the world's X-axis, then enter (0,90,0) for  <strong>Alpha</strong>, <strong>Beta</strong> and <strong>Gamma</strong>. All joints have the right position and orientation now. <br>
</p>

<p>You can now adjust the joint sizes (check the <strong>Joint length</strong> and <strong>Joint diameter</strong> items) in the <a href="jointProperties.htm">joint properties</a> dialog (that you can open by double-clicking a joint icon in the scene hierarchy). Make sure that all joints are clearly visible. This is what you should have:<br>
</p>

<p align=center><img src="images/ikTut5.jpg"></p>
<br>

<p>The next step in this tutorial is to group shapes that belong to the same rigid entity. Select the 5 shapes that are part of link 1 (the base cylinder being &quot;link 0&quot;), then click [Menu bar --&gt; Edit --&gt; Grouping/Merging --&gt; Group selected shapes]. Once the shapes are grouped in a compound shape, you could re-align its bounding box with the world, but this step is not required (and has only a visual effect). Repeat the same procedure with all shapes that logically belong together. In this tutorial we will not actuate the gripper's fingers, and so simply rigidly group them with the last link. When all shapes that are meant to be grouped share the same visual attributes, try merging them together instead ([Menu bar --&gt; Edit --&gt; Grouping/Merging --&gt; Merge selected shapes]).<br>
</p>

<p>At this point you can rename all objects in the scene in following way, when going from base to tip: &quot;redundantRobot&quot; -  &quot;redundantRob_joint1&quot;  - &quot;redundantRob_link1&quot; - &quot;redundantRob_joint2&quot;, etc. Just double-click an object's name in the scene hierarchy to edit its name.<br>
</p>

<p>Now we can build the kinematic chain, going from tip to base: select object &quot;redundantRob_link7&quot;, then ctrl-select object &quot;redundantRob_joint7&quot; and click [Menu bar --&gt; Edit --&gt; Make last selected object parent]. Alternatively you can drag an object onto another one in the scene hierarchy to achieve a similar operation. Next do the same for object &quot;redundantRob_joint7&quot; and object &quot;redundantRob_link6&quot;. Continue in a same way until the whole kinematic chain of the manipulator was built. This is what you should have (notice the scene hierarchy's structure):<br>
</p>

<p align=center><img src="images/ikTut6.jpg"></p>
<br>

<p>Select all joints, then in the joint dialog, select &quot;joint is in inverse kinematics mode&quot; in the <strong>Joint mode</strong> section, then click <strong>Apply to selection</strong>. Then open the <a href="commonPropertiesDialog.htm">object common properties</a> and in the<strong> Visibility layers</strong> section, disable layer 2 and enable layer 10, then click the related <strong>Apply to selection</strong> button. This just sent all joints to the visibility layer 10, effectively making them invisible. Have a look at the <a href="layerSelectionDialog.htm">layer selection dialog</a> if you wish to temporarily enable/disable some layers.<br>
</p>

<p>Now we will define an inverse kinematics task for the manipulator. In CoppeliaSim, an IK task requires specification of at least following elements: <br>
</p>
<li>a kinematic chain described with a &quot;tip&quot; dummy and a &quot;base&quot; object.
</li>

<li>A &quot;target&quot; dummy that the &quot;tip&quot; dummy will be constrained to follow.
</li>

<p>We already have the &quot;base&quot; object (object &quot;redundantRobot&quot;). Let's add a <a href="dummies.htm">dummy object</a>, rename it to &quot;redundantRob_tip&quot; and set its position to (0.324,0,0.62) using the coordinate and transformation dialog. Next, attach the dummy to &quot;redundantRob_link7&quot; (select &quot;redundantRob_tip&quot;, then &quot;redundantRob_link7&quot;, then click [Menu bar --&gt; Edit --&gt; Make last selected object parent]. Our &quot;tip&quot; dummy is ready!<br>
</p>

<p>Now let's prepare the &quot;target&quot; dummy: copy and paste &quot;redundantRob_tip&quot; and rename the copy to &quot;redundantRob_target&quot;. The &quot;target&quot; dummy is ready. Next, we have to inform CoppeliaSim that &quot;redundantRob_tip&quot; and &quot;redundantRob_target&quot; are a <em>tip-target</em> pair for inverse kinematics resolution. Double-click the dummy icon of &quot;redundantRob_tip&quot; in the scene hierarchy: this opens the <a href="dummyPropertiesDialog.htm">dummy properties</a> dialog. In the <strong>Dummy-dummy linking</strong> section, specify &quot;redundantRob_target&quot; as <strong>Linked dummy</strong>. Notice how both dummies got linked through a red stippled line in the scene hierarchy (the two dummies are also linked in the scene through a red line, but since both dummies are coincident, the line cannot be seen). In the same dialog, <strong>Link type</strong> is already <strong>IK, tip-target</strong>, which is the default value. This is what you should have by now: <br>
</p>


<p align=center><img src="images/ikTut7.jpg"></p>
<br>

<p>At this stage, all elements for the definition of the inverse kinematics task are ready, and we just need to register the task as an <a href="basicsOnIkGroupsAndIkElements.htm"><em>IK group</em></a>. Open the <a href="ikDialog.htm">inverse kinematics dialog</a> and click <strong>Add new IK group</strong>. A new item appears in the <strong>IK groups</strong> list: &quot;IK_Group&quot;. While that item is selected, click <strong>Edit IK elements</strong> to open the <a href="ikElementDialog.htm">IK element dialog</a>. Next to the <strong>Add new IK element with tip </strong>button, select &quot;redundantRob_tip&quot; in the drop-down box, then click the <strong>Add new IK element with tip</strong> button. This just added an IK element that appears in the list. Further down, indicate &quot;redundantRobot&quot; as the <strong>Base</strong>. Finally, make sure that all items are checked in the <strong>Constraints</strong> section (check als <strong>Alpha-Beta</strong> and <strong>Gamma</strong>). Indeed, we want our &quot;tip&quot; dummy to follow our &quot;target&quot; dummy in position and orientation:</p>

<p align=center><img src="images/ikTut8b.jpg"></p>
<br>

<p> Close the IK element dialog. In the inverse kinematics dialog, you are free to check the item <strong>Mechanism is redundant</strong>, but at this stage, it won't make any difference since no joint limits or obstacle avoidance parameters have been defined.<br>
</p>

<p align=center><img src="images/ikTut8.jpg"></p>
<br>

<p>Our inverse kinematics task is ready! Let's test it. Run the simulation, then select &quot;redundantRob_target&quot;. Next,  select the object translation toolbar button:<br>
</p>
<p align=center><img src="images/objectShiftButton.jpg"></p>
<br>

<p>Now drag the object with the mouse: the manipulator should follow. Also try the object rotation toolbar button:<br>
</p>

<p align=center><img src="images/objectRotateButton.jpg"></p>
<br>

<p>Try also holding down the ctr- or shift-keys during manipulation. Switch back to the object translation toolbar button, and try to drag the object as far as possible, and notice how the inverse kinematics task <em>breaks</em>. Indeed, this happens when a configuration is singular or not reachable, however there are workarounds to this behavior: while the simulation is still running, select &quot;IK_Group&quot; in the inverse kinematics dialog list, then specify <strong>DLS</strong> for the <strong>Calc. method</strong> item. Drag the object out of reach and notice how the inverse kinematics resolution became more stable. Try adjusting the <strong>Damping</strong> item up and down. Basically, when damping is large, resolution becomes more stable but slower. Practically, you can get the advantages of both resolution methods, all you need to do is define two identical &quot;IK groups&quot;, where the first is not damped and the second is damped. Then, for the second &quot;IK group&quot;, you can specify a conditional resolution (refer to the <strong>Edit conditional parameters</strong> item for more information). Now set back the <strong>Calc. method</strong> item to <strong>Pseudo inverse</strong>.<br>
</p>

<p>Now, in the <a href="ikElementDialog.htm">IK element dialog</a>, select &quot;redundantRob_tip&quot; then try to disable some of the <strong>Constraints</strong> items and notice how the manipulator behaves when the &quot;redundantRob_target&quot; object is dragged or rotated. Once you experimentated enough, reset all <strong>Constraints</strong> items to &quot;checked&quot;, then stop the simulation.<br>
</p>

<p>What we will now do is add a way to easily manipulate the robot, without having to worry about breaking it by shifting the wrong objects around. We will therefore define it as a <a href="models.htm">model</a>. First, move &quot;redundantRob_tip&quot; and &quot;redundantRob_target&quot; to layer 11 to make both dummies invisible. Then shift-select all visible objects in the scene view, ctrl-click the object &quot;redundantRobot&quot; in the scene hierarchy to remove it from the selection, then open the <a href="commonPropertiesDialog.htm">object common properties</a> dialog. Check the <strong>Select base of model instead</strong> item, then the related <strong>Apply to selection</strong> button. Clear the selection with &lt;ESC&gt;, then select &quot;redundantRobot&quot;. In the same dialog, check the <strong>Object is model base</strong> item, then close the dialogs. Notice how a stippled bounding box now encompasses the whole manipulator:<br>
</p>

<p align=center><img src="images/ikTut9.jpg"></p>
<br>

<p>Click any object on the manipulator and notice how the base dummy (&quot;redundantRobot&quot;) always gets selected instead.<br>
</p>

<p>Next, let's add a &quot;manipulation sphere&quot; that we will use to manipulate the robot's gripper position/orientation. Click [Menu bar --&gt; Add --&gt; Primitive shape --&gt; Sphere] to open the <a href="primitiveShapes.htm">primitive shape dialog</a>, indicate 0.05 for the <strong>X-Size</strong>, <strong>Y-Size</strong> and <strong>Z-Size</strong>, then uncheck the <strong>Create dynamic and respondable shape</strong> item and click <strong>OK</strong>. Adjust the newly added sphere's position to be the same as &quot;redundantRob_target&quot; (using the coordinate and transformation dialog). The sphere now appears at the tip of the manipulator. Rename the sphere to &quot;redundantRob_manipSphere&quot;, then make it parent of &quot;redundantRob_target&quot;. When you now run the simulation, you should be able to change the manipulator's configuration by moving the manipulation sphere around. Stop the simulation again.<br>
</p>

<p>Let's change a few other details. In the <a href="shapeProperties.htm">shape properties</a> dialog, click <strong>Adjust outside color</strong>, then check the <strong>Opacity</strong> item. Notice how the sphere appearance changed. For a nicer appearance, check the <strong>Backface culling</strong> item in the shape dialog.<br>
</p>

<p>In the object common properties dialog uncheck all items in the <strong>Object special properties</strong> section (this is because the manipulation sphere doesn't really belong to the manipulator, it is more of a user-interface element). Now make &quot;redundantRobot&quot; parent of &quot;redundantRob_manipSphere&quot;:<br>
</p>

<p align=center><img src="images/ikTut11.jpg"></p>
<br>

<p>In the final step of this tutorial, we will register a <strong>collision object</strong> that should <a href="collisionDetection.htm">detect collisions</a> between the manipulator and its environment. What we want is for every single shape (except for the manipulation sphere) in the manipulator to be able to detect a collision with the environment. Let's first define a <a href="collections.htm">collection</a> for our manipulator:<br>
</p>

<p>Open the <a href="collectionsDialog.htm">collection dialog</a> with [Menu bar --&gt; Tools --&gt; Collections] or by clicking the appropriate toolbar button. Select &quot;redundantRobot&quot;, then click <strong>Add new collection</strong>. A new, empty collection was added. We now need to define the collection content: click <strong>Add</strong> (make sure &quot;redundantRobot&quot; is still selected). Notice how the collection's content changed. Now select newly added collection item, then click <strong>Visualize selected collection</strong>: all objects composing the manipulator get a pink color in the scene! Rename the collection to &quot;redundantRob&quot;. This is what you should have:<br>
</p>

<p align=center><img src="images/ikTut12.jpg"></p>
<br>

<p>Now that the &quot;redundantRob&quot; collection is defined, we can register a collision object: open the <a href="collisionDetection.htm">collision dialog</a>  then click <strong>Add new collision object</strong> and specify the following item pair: &quot;[Collection] redundantRob&quot; - &quot;all other collidable objects in the scene&quot;. This added a new collision object that you can rename in the list with a double-click (rename it to &quot;redundantRob&quot;). <br>
</p>


<p>Collapse the &quot;redundantRobot&quot; tree in the scene hierarchy. Your redundant manipulator model is ready!<br>
</p>


<p>Run the simulation, and copy-paste a few times the manipulator. Shift/rotate the copies around, and change their configurations by dragging their manipulation spheres. Notice how every robot instance is fully functional, and how collisions are indicated with a color change. Open the inverse kinematics dialog, the collection dialog and the collision detection dialog. Notice how the listed items have also been automatically duplicated. Stop the simulation.<br>
</p>


<p>The procedure to register a <a href="distanceCalculation.htm">minimum distance object</a> is very similar to the collision object registration here above. All registered objects (collision detection, collections, IK groups, etc.) and all scene objects can be accessed through appropriate <a href="apisOverview.htm">API</a> calls. Additionally, they can be directly recorded and visualized by <a href="graphs.htm">graph objects</a>.<br>
</p>

<p align=center><img src="images/ikTut13.jpg"></p>






<br>
<br>

 </tr>
</table> 
</div>  
  
  
</body>

</html>
